import { director, sys } from "cc";
import { showToast } from "../utils";
import { userStore } from "./user";
import { useSocket } from "./socket";
let code = "";
let button
const wx = window['wx']
export async function setButton(value) {
    button = value
    button.onTap((res) => {
        console.log(res, "setButton")
        if (res.errno) {
            console.log("拒绝授权")
            return
        }
        button.hide()
        userStore().login().then(() => {
            // showToast("登录成功")
            useSocket().connect()
            setTimeout(() => {
                director.loadScene('home')
            }, 600)
        }).catch(err => { })
    })

}
export async function getCode() {
    return new Promise((resolve) => {
        wx.login({
            provider: "weixin",
            success: (res) => {
                console.log(res.code, "getCode")
                code = res.code;
                console.log(code, "getCode")
                resolve(res.code);
            },
        });
    });
}
export async function shareApp(query) {
    wx.shareAppMessage({
        title: '快来和我一起帮助小妖精逃出地牢吧~',
        imageUrl: "https://image-apk-1310343798.cos.ap-nanjing.myqcloud.com/app%2Fbase%2Fb77b7449739e4c748385afa08396db12_1288.png",
        query,
    });
}
export async function miniShare(query) {
    wx.showShareMenu({
        withShareTicket: false,
        // 显示菜单
        menus: ['shareAppMessage', 'shareTimeline']
    });
    // 好友点击回调
    wx.onShareAppMessage(() => {
        return {
            title: '快来和我一起帮助小妖精逃出地牢吧~',
            imageUrl: "https://image-apk-1310343798.cos.ap-nanjing.myqcloud.com/app%2Fbase%2Fb77b7449739e4c748385afa08396db12_1288.png",
            query,
            success: function (res) {
                // 转发成功
                wx.showToast({
                    title: '分享成功',
                })
            },
            fail: function (res) {
                console.log('分享失败', res)
            }
        };
    });
    // 朋友圈点击回调
    wx.onShareTimeline(() => {
        return {
            title: '快来和我一起帮助小妖精逃出地牢吧'
        };
    });
}
export async function miniLogin(): Promise<{ code: string;[key: string]: any }> {
    return new Promise(async (resolve, reject) => {
        if (code == "") {
            await getCode();
        }
        wx.getSetting({
            success(res) {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                    wx.getUserInfo({
                        lang: "zh_CN",
                        desc: "授权信息仅用于用户登录",
                        success({ iv, encryptedData, signature, rawData }) {
                            console.log(code, "code")
                            function next() {
                                resolve({
                                    iv,
                                    encryptedData,
                                    signature,
                                    rawData,
                                    code,
                                });
                                code = ""
                            }
                            // 检查登录状态是否过期
                            wx.checkSession({
                                success() {
                                    console.log("checkSession")
                                    next();
                                },
                                fail() {
                                    console.log('checkSession fail')
                                    getCode().then(next);
                                },
                            });
                        },
                        fail(err) {
                            console.error(err.errMsg, "err");
                            showToast("登录授权失败")
                            getCode();
                            reject({
                                message: "登录授权失败",
                            });
                        },
                    });
                }
            }
        })
    });
}